home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / ccs_util.jar / test / ui / TestStatus.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-12-09  |  2.6 KB  |  85 lines

  1. package test.ui;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Graphics;
  7. import java.awt.Rectangle;
  8. import java.awt.SystemColor;
  9.  
  10. class TestStatus extends Canvas {
  11.    public boolean fError = false;
  12.    public int fTotal;
  13.    public int fProgress;
  14.    public int fProgressX;
  15.  
  16.    public TestStatus() {
  17.       ((Component)this).setSize(20, 30);
  18.    }
  19.  
  20.    private Color getStatusColor() {
  21.       return this.fError ? Color.red : Color.green;
  22.    }
  23.  
  24.    public void paint(Graphics g) {
  25.       this.paintBackground(g);
  26.       this.paintStatus(g);
  27.    }
  28.  
  29.    public void paintBackground(Graphics g) {
  30.       g.setColor(SystemColor.control);
  31.       g.fillRect(0, 0, ((Component)this).getBounds().width, ((Component)this).getBounds().height);
  32.       g.setColor(Color.darkGray);
  33.       g.drawLine(0, 0, ((Component)this).getBounds().width - 1, 0);
  34.       g.drawLine(0, 0, 0, ((Component)this).getBounds().height - 1);
  35.       g.setColor(Color.white);
  36.       g.drawLine(((Component)this).getBounds().width - 1, 0, ((Component)this).getBounds().width - 1, ((Component)this).getBounds().height - 1);
  37.       g.drawLine(0, ((Component)this).getBounds().height - 1, ((Component)this).getBounds().width - 1, ((Component)this).getBounds().height - 1);
  38.    }
  39.  
  40.    public void paintStatus(Graphics g) {
  41.       g.setColor(this.getStatusColor());
  42.       Rectangle r = new Rectangle(0, 0, this.fProgressX, ((Component)this).getBounds().height);
  43.       g.fillRect(1, 1, r.width - 1, r.height - 2);
  44.    }
  45.  
  46.    private void paintStep(int startX, int endX) {
  47.       Graphics g = ((Component)this).getGraphics();
  48.       g.setColor(this.getStatusColor());
  49.       g.fillRect(startX, 1, endX - startX, ((Component)this).getBounds().height - 2);
  50.    }
  51.  
  52.    public void reset() {
  53.       this.fProgressX = 1;
  54.       this.fProgress = 0;
  55.       this.fError = false;
  56.       this.paint(((Component)this).getGraphics());
  57.    }
  58.  
  59.    public int scale(int value) {
  60.       return this.fTotal > 0 ? Math.max(1, value * (((Component)this).getBounds().width - 1) / this.fTotal) : value;
  61.    }
  62.  
  63.    public void setBounds(int x, int y, int w, int h) {
  64.       super.setBounds(x, y, w, h);
  65.       this.fProgressX = this.scale(this.fProgress);
  66.    }
  67.  
  68.    public void start(int total) {
  69.       this.fTotal = total;
  70.       this.reset();
  71.    }
  72.  
  73.    public void step(boolean successful) {
  74.       ++this.fProgress;
  75.       int x = this.fProgressX;
  76.       this.fProgressX = this.scale(this.fProgress);
  77.       if (!this.fError && !successful) {
  78.          this.fError = true;
  79.          x = 1;
  80.       }
  81.  
  82.       this.paintStep(x, this.fProgressX);
  83.    }
  84. }
  85.